//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "ddk.h"
#include <errno.h>
#include "CDirStrEnumerator.h"
#define IncrementDllLockCount() do {} while(0)
#define DecrementDllLockCount() do {} while(0)
#include "_CKtfsDirStrEnumerator.cpp"
#include <string.h>

ECode CKtfsDirStrEnumerator::Current(
                /* [out] */ WStringBuf * pPsa)
{
    if (pPsa == NULL || !pPsa->GetCapacity()) {
        return E_INVALID_ARGUMENT;
    }
    if (m_bReset) {
        return E_INVALID_OPERATION;
    }
    pPsa->Copy(m_wcsFileName);
    return NOERROR;
}

ECode CKtfsDirStrEnumerator::MoveNext()
{
    m_bReset = FALSE;
    IO_FIND_DATA FindFileData;
    if (!m_bMove) {
        m_hFind = IO_FindFirstFile (m_esPathName, &FindFileData);
        if (m_hFind == IO_INVALID_HANDLE_VALUE) {
            return E_FAIL;
        }
        m_bMove = TRUE;
    }
    else {
        if (m_hFind == IO_INVALID_HANDLE_VALUE ||
            !IO_FindNextFile (m_hFind, &FindFileData)) {
            return S_FALSE;
        }
    }
    wcscpy(m_wcsFileName, FindFileData.cFileName);
    return NOERROR;
}

ECode CKtfsDirStrEnumerator::Reset()
{
    m_bReset = TRUE;
    m_bMove = FALSE;
    IO_FindClose(m_hFind);
    m_hFind = IO_INVALID_HANDLE_VALUE;

    return NOERROR;
}
